from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

#from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
# from dwebsocket.decorators import accept_websocket
import uuid


def myindex(request):

    return HttpResponse('这里是首页')

#类视图 可读性好 易于扩展
# class MyView(View):
#     greeting = '你好，这里是首页'

#     def get(self,request):
#         title = 'hello django'
#         num = 100 
#         li = [1,2,3,4,5]
#         dic = {"name":'小红','age':19}
#         # return HttpResponse(self.greeting)
#         return render(request,'index.html',locals())

    # def post(self,request):
    #     return HttpResponse(self.greeting)

from datetime import datetime
from myapp.models import User


# 所有
def index(request):
    user = User.objects.all()
    return render(request, 'index.html', locals())



from mydjango.settings import STATICFILES_DIRS
class add(View):
    def get(self, request):
        return render(request, 'add.html', locals())

    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        img = request.FILES.get('img')
        type = request.POST.get('type')
        if not all([username, password]):
            return render(request, 'index.html', {'message': '请输入完整'})
        # 添加头像
        img_name = datetime.now().strftime('%Y%m%d%H%M%S') + img.name
        img_path = os.path.join(STATICFILES_DIRS[0], 'upload', img_name)
        f = open(img_path, 'wb')
        for i in img.chunks():
            f.write(i)
        f.close()

        u=User()
        u.username=username
        u.password=password
        u.img='/static/upload/'+img_name
        u.type=type
        u.save()
        return redirect('/')
# 修改
class modify(View):
    def get(self,request):
        id= request.GET.get('id')
        user=User.objects.filter(pk=id).first()
        return render(request,'modify.html',locals())
    def post(self,request):
        id = request.GET.get('id')
        username = request.POST.get('username')
        password = request.POST.get('password')
        img = request.FILES.get('img')
        type = request.POST.get('type')
        img_name = datetime.now().strftime('%Y%m%d%H%M%S') + img.name
        img_path = os.path.join(STATICFILES_DIRS[0], 'upload', img_name)
        f = open(img_path, 'wb')
        for i in img.chunks():
            f.write(i)
        f.close()
        if id:
            user=User.objects.filter(id=id).first()
            user.username=username
            user.password=password
            user.img='/static/upload/'+img_name
            user.type=type
            user.save()
            return redirect('/')
        return render(request,'modify.html',locals())

# 删除
class delete(View):
    def get(self,request):
        id= request.GET.get('id')
        user_id=User.objects.filter(pk=id).first()
        user_id.delete()
        return redirect('/')


# 搜索
# class sou(View):
#     def get(self,request):
#         username=request.GET.get('username')
#         msg=''
#         if not username:
#             msg='请输入用户名查找'
#             return render(request,'index.html',locals())
#         user_list=models.User.objects.filter(username==username)
#         return render(request,'sou.html',locals())